package taskTracker.util;

import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;

import taskTracker.ReportsManager.TimeSummary;

import javax.microedition.lcdui.Graphics;

import com.sun.perseus.j2d.RGB;
import com.sun.perseus.parser.ColorParser;

public class DrawingManager {
	private static DrawingManager instatnce = null;

	private DrawingManager() {
	}

	public static DrawingManager getInstance() {
		if (instatnce == null) {
			instatnce = new DrawingManager();
		}
		return instatnce;
	}

	public void drawPie(Graphics graphics, int areaWidth, Enumeration objects) {
		// Get total value of all slices
		long total = 0;
		Vector slices = new Vector();

		while (objects.hasMoreElements()) {
			TimeSummary obj = (TimeSummary)objects.nextElement();
			slices.addElement(obj);
			total += obj.duration;
		}

		// Draw each pie slice
		double curValue = 0.0D;
		int startAngle = 0;
		
		Random rand = new Random();

		for (int i = 0; i < slices.size(); i++) {
			// Compute the start and stop angles
			startAngle = (int) (curValue * 360 / total);
			int arcAngle = (int) (((TimeSummary)slices.elementAt(i)).duration * 360 / total);

			// Ensure that rounding errors do not leave a gap between the first
			// and last slice
			if (i == slices.size() - 1) {
				arcAngle = 360 - startAngle;
			}

			// Set the color and draw a filled arc
			//g.setColor(slices[i].color);
			graphics.setColor(rand.nextInt());
			graphics.fillArc(0, 0, areaWidth, areaWidth, startAngle, arcAngle);
			curValue += ((TimeSummary)slices.elementAt(i)).duration;
		}
	}
}
